<?php
/**
 * CK 快速开发框架
 * User: Clake
 * Date: 15/6/2
 * Time: 03:16
 */

namespace CK\Core;

use \CK\Util\IO\File;
use \CK\Util\Arr;
/**
 * Class Config
 * 核心配置类
 * @package CK\Core
 */
class Config extends Base{

    private static $_instance = [];

    private $_conf_dir = APP_CONF_PATH;

    private $_data = [];

    private $_conf = [
        'extens'=>'.conf.php',
        'delimiter'=>'.'
    ];

    /**
     * 实例化配置类
     * @param string $name 配置文件名
     * @param null   $conf_dir  配置文件目录,默认app目录下
     *
     * @return Config
     */
    public static function inst($name=null,$conf_dir=null) {
        if (!isset(self::$_instance[$name])) {
            self::$_instance[$name] = new self($name,$conf_dir);
        }
        return self::$_instance[$name];
    }

    public function __construct($conf_name,$conf_dir=null) {
        if ($conf_dir) {
            $this->_conf_dir = $conf_dir;
        }
        if (File::Exists($this->_conf_dir.$conf_name.$this->_conf['extens'])) {
            $this->_data = require($this->_conf_dir.$conf_name.$this->_conf['extens']);
        }
    }

    /**
     * 得到一个配置文件节点值
     *
     * @param $path 节点名称可用点分割 examlpe:foo.bar
     *
     * @return null
     */
    public function get($path=null) {
        if (empty($path)) {
            return $this->_data;
        }
        return Arr::get($this->_data,$path,$this->_conf['delimiter']);
    }

    /**
     * 设置一个配置文件值
     * @param $path
     * @param $value
     */
    public function set($path,$value) {
        Arr::set($this->_data,$path,$value,$this->_conf['delimiter']);
    }
}